﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace AUV5.Web.Pages
{
    public partial class UCService : System.Web.UI.UserControl
    {
        public string ServiceID { get; set; }

        public bool IsList { get; set; }

        public bool IsWhiteBg { get; set; }

        protected bool favoriteVisible = false;

        public PageBaseTop pagebase = new PageBaseTop();

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            BindData(ServiceID);
        }

        #region##绑定服务信息
        /// <summary>
        /// 绑定服务信息
        /// </summary>
        /// <param name="serviceId"></param>
        protected void BindData(string serviceId)
        {
            //服务信息
            AUV5.Model.ServiceInfo service = AUV5.BLL.ServiceInfoBLL.Instance.GetDetail(serviceId);

            if (service != null)
            {
                //更新浏览量
                service.ViewCount++;

                AUV5.BLL.ServiceInfoBLL.Instance.UpdateServiceInfo(service);

                //卖家信息
                AUV5.Model.UserInfo user = AUV5.BLL.UserInfoBLL.Instance.GetDetail(service.UserID);

                if (user != null)
                {
                    int totalRateCount = AUV5.BLL.ServiceInfoBLL.Instance.GetRateSumByUserId(user.UserID);
                    if (totalRateCount > 0)
                    {
                        double receRate = Convert.ToDouble(AUV5.BLL.ServiceInfoBLL.Instance.GetGoodRateSumByUserId(user.UserID)) / Convert.ToDouble(totalRateCount);
                        lblSellerReceivedRate.Text = receRate.ToString("0%");
                    }
                    else
                    {
                        lblSellerReceivedRate.Text = "-";
                    }

                    //lbtnSellerUserName.PostBackUrl = "UserHome.aspx?username=" + user.UserName;

                    string contractUrl = "~/Pages/MessageShow.aspx?userID=" + user.UserID + "&serviceID=" + serviceId;

                    if (pagebase.IsVerified)
                    {
                        lbtnContractSeller.NavigateUrl = contractUrl;
                    }
                    else
                    {
                        lbtnContractSeller.NavigateUrl = "~/Pages/Login.aspx?returnURL=" + contractUrl;
                    }
                }


                //-----------------------------------------以下是服务信息显示部分------------------------------------------------

                hlServiceTitle.Text = service.Title;            //服务标题
                if (IsList)
                {
                    hlServiceTitle.NavigateUrl = "/service/" + service.Title;
                }
                else
                {
                    hlServiceTitle.Enabled = false;
                }
                //lbPrice.Text = service.Price.ToString();  //服务价格
                lbtnOrder.Text = service.Price.ToString("0");  //服务价格
                int totalRate = service.GoodRating + service.BadRating;
                if (totalRate != 0)                                          //好评率
                {
                    double receRate = Convert.ToDouble(service.GoodRating) / Convert.ToDouble(totalRate);
                    lblReceivedRate.Text = receRate.ToString("0%");
                }
                else
                {
                    lblReceivedRate.Text = "-";
                }

                lblGoodRate.Text = service.GoodRating.ToString();  //好评

                lblBadRate.Text = service.BadRating.ToString();   //差评

                lblDelivery.Text = service.Duration.ToString();   //有效时间

                if (service.EndDate > DateTime.Now)
                {
                    if (DateTime.MaxValue.Year == service.EndDate.Year)
                    {
                        lbLastDay.Text = "-";
                    }
                    else
                    {
                        lbLastDay.Text = ((int)(service.EndDate - DateTime.Now).TotalDays).ToString();
                    }
                }
                else
                {
                    lbLastDay.Text = "0";
                }

                if (service.NeedStock)  //库存
                {
                    lbStock.Text = service.Stock.ToString();
                }
                else
                {
                    lbStock.Text = "不限";
                }
                int queueCount = AUV5.BLL.OrderItemBLL.Instance.GetOrderItemQuantityCountByCondition(serviceId, Convert.ToInt32(AUV5.Common.Enum.OrderState.Payment));

                lblQueueCount.Text = queueCount.ToString();             //队列

                lblJoin.Text = pagebase.GetTimeSpan(service.CreateDate);   //加入时间

                lblServiceDesc.Text = service.Description;         //服务描述

                lblCreateTime.Text = pagebase.GetTimeSpan(service.CreateDate);  //底部创建时间

                lblFavCount.Text = Convert.ToString(AUV5.BLL.FavoriteBLL.Instance.GetSFavCountByServiceId(service.ServiceID));  //多少人喜欢

                lblFbUserCount.Text = Convert.ToString(AUV5.BLL.FeedBackBLL.Instance.GetFeebBackDistinct(service.ServiceID));   //多少人分享了经验

                if (pagebase.IsVerified)                               //收藏
                {
                    if (pagebase.VerifiedUserID == service.UserID)
                    {
                        lbtnContractSeller.Visible = false;
                        lbtnOrder.Visible = false;
                        imgBtnEdit.Visible = true;
                        imgBtnEdit.PostBackUrl = "~/Pages/ServiceEdit.aspx?serviceID=" + serviceId;
                    }
                    //库存为0则禁用
                    if (service.NeedStock && service.Stock < 1)
                    {
                        lbtnOrder.OnClientClick = "alert('没货了……亲！'); return false;";
                    }
                }
                else
                {
                    //lbtnCollection.Visible = false;
                }

                BindKeyRelated(service.ServiceID);  //关键字绑定

                bindAttach(service.ServiceID);  //附件绑定

                BindExtra(service.ServiceID);//绑定增值服务   via Vinlon
            }
        }
        #endregion

        #region##绑定关键字
        /// <summary>
        /// 绑定关键字
        /// </summary>
        private void BindKeyRelated(string serviceId)
        {
            rpKeys.DataSource = AUV5.BLL.KeywordBLL.Instance.GetKeyListByServiceId(serviceId);
            rpKeys.DataBind();
        }
        #endregion

        #region   增值服务
        private void BindExtra(string serviceid)
        {
            IList<Model.ExtraService> list = BLL.ExtraServiceBLL.Instance.GetDetailByServiceID(serviceid);
            if (list.Count > 0 && pagebase.ExtraService_Enabled)
            {
                repExtraService.DataSource = list;
                repExtraService.DataBind();
            }
            else
            {
                extraPanel.Visible = false;
            }
        }
        //选择Extra Service
        protected void selectExtra_CheckedChanged(object sender, EventArgs e)
        {
            Model.ServiceInfo service = BLL.ServiceInfoBLL.Instance.GetDetail(ServiceID);
            if (service != null)
            {
                ArrayList extralist = new ArrayList();
                foreach (RepeaterItem item in repExtraService.Items)
                {
                    string extraid = ((HiddenField)item.FindControl("hfExtraID")).Value;
                    CheckBox selectExtra = (CheckBox)item.FindControl("selectExtra");
                    if (selectExtra.Checked)
                    {
                        extralist.Add(extraid);
                    }
                    else
                    {
                        extralist.Remove(extraid);
                    }
                }
                ViewState["extralist"] = extralist;
                int count = ((ArrayList)ViewState["extralist"]).Count;
                orderWithExtra.Visible = count > 0 && pagebase.VerifiedUserID != service.UserID;
            }
        }
        #endregion

        #region##绑定服务附件
        /// <summary>
        /// 绑定服务附件
        /// </summary>
        /// <param name="serviceID"></param>
        private void bindAttach(string serviceID)
        {
            repAttach.DataSource = BLL.ServiceAttachmentBLL.Instance.GetListByServiceID(4, serviceID);
            repAttach.DataBind();
        }
        #endregion

        #region##订单
        /// <summary>
        /// 订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnOrder_Click(object sender, EventArgs e)
        {
            try
            {
                if (pagebase.IsVerified)
                {
                    string returnurl = BLL.OrderInfoBLL.Instance.OrderService(ServiceID, pagebase.VerifiedUserID, null);
                    Response.Redirect(returnurl, false);
                }
                else
                {
                    Response.Redirect("~/Pages/Login.aspx?returnURL=" + Request.Url);
                }
            }
            catch (Exception)
            {
            }
        }


        //包括增值服务
        protected void orderWithExtra_Click(object sender, EventArgs e)
        {
            try
            {
                if (pagebase.IsVerified)
                {
                    string returnurl = BLL.OrderInfoBLL.Instance.OrderService(ServiceID, pagebase.VerifiedUserID, (ArrayList)ViewState["extralist"]);
                    Response.Redirect(returnurl);
                }
                else
                {
                    //base.Login();
                    Response.Redirect("~/Pages/Login.aspx?returnURL=" + Request.Url);
                }
            }
            catch (Exception)
            {
                //log.Error("Order With Extra Error");
                throw;
            }
        }
        #endregion
    }
}